<!--物料分级分类-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="160px">
      <el-form-item label="序号" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入序号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="编码前4位" prop="codeBefore4">
        <el-input v-model="queryParams.codeBefore4" placeholder="请输入编码前4位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料L1" prop="materialClass1">
        <el-input v-model="queryParams.materialClass1" placeholder="请输入物料L1" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料子类L2" prop="itemSubclass2">
        <el-input v-model="queryParams.itemSubclass2" placeholder="请输入物料子类L2" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料子类L3" prop="itemSubclass3">
        <el-input v-model="queryParams.itemSubclass3" placeholder="请输入物料子类L3" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备注2" prop="materialSubclassOld4">
        <el-input v-model="queryParams.materialSubclassOld4" placeholder="请输入备注2" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料小类L4" prop="materialSubclass4">
        <el-input v-model="queryParams.materialSubclass4" placeholder="请输入物料小类L4" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="英文" prop="english">
        <el-input v-model="queryParams.english" placeholder="请输入英文" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料组代码" prop="groupCode">
        <el-input v-model="queryParams.groupCode" placeholder="请输入物料组代码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备注1" prop="remarkOne">
        <el-input v-model="queryParams.remarkOne" placeholder="请输入备注1" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="大类(中文)" prop="bigClassChinese">
        <el-input v-model="queryParams.bigClassChinese" placeholder="请输入大类(中文)" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="小类(中文)" prop="smallClassChinese">
        <el-input v-model="queryParams.smallClassChinese" placeholder="请输入小类(中文)" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="大类" prop="bigClass">
        <el-input v-model="queryParams.bigClass" placeholder="请输入大类" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="小类" prop="smallClass">
        <el-input v-model="queryParams.smallClass" placeholder="请输入小类" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="次小类" prop="subCategory">
        <el-input v-model="queryParams.subCategory" placeholder="请输入次小类" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['gyl:materials:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['gyl:materials:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['gyl:materials:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['gyl:materials:export']">导出</el-button>
      </el-col>
      <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
      <EasyTable ref="childRef" :tableData="materialsList" :columns="tableColumn" :Shuju="Shuju"  @listen="handleSelectionChange" @changeList="rowsol">
        <vxe-column title="操作" width="200" show-overflow>
          <template #default="{ row }">
            <vxe-button type="text" icon="vxe-icon-edit" @click="handleUpdate(row)">编辑</vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" @click="handleDelete(row)">删除</vxe-button>
          </template>
        </vxe-column>
      </EasyTable>
    </el-row>
    <div style="display: flex; height: 30px; justify-content:flex-end">
      <div class="Size">字体大小</div>
      <el-input class="Einput" v-model="Shuju.fontSize" placeholder="字体大小" clearable @blur="blurs"></el-input>
      <div class="Size">行高</div>
      <el-input class="Einput" v-model="Shuju.Height" placeholder="行高" clearable @blur="blurs">
      </el-input>
      <pagination v-show="total > 0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList" style="width: 800px ; margin-top: -10px"/>
    </div>
<!--    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList" />-->
    <!--    修改某一列数据-->
    <el-dialog :visible.sync="opens" width="500px" append-to-body :title="title">
      <el-form label-width="80px">
        <el-form-item label="修改值" prop="category">
          <el-input v-model="list.afterName" placeholder="请输入修改值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitList">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改物料分级分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序号" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="编码前4位" prop="codeBefore4">
          <el-input v-model="form.codeBefore4" placeholder="请输入编码前4位" />
        </el-form-item>
        <el-form-item label="物料L1" prop="materialClass1">
          <el-input v-model="form.materialClass1" placeholder="请输入物料L1" />
        </el-form-item>
        <el-form-item label="物料子类L2" prop="itemSubclass2">
          <el-input v-model="form.itemSubclass2" placeholder="请输入物料子类L2" />
        </el-form-item>
        <el-form-item label="物料子类L3" prop="itemSubclass3">
          <el-input v-model="form.itemSubclass3" placeholder="请输入物料子类L3" />
        </el-form-item>
        <el-form-item label="备注2" prop="remarkTwo">
          <el-input v-model="form.materialSubclassOld4" placeholder="请输入物料小类L4[old]" />
        </el-form-item>
        <el-form-item label="物料小类L4" prop="materialSubclass4">
          <el-input v-model="form.materialSubclass4" placeholder="请输入物料小类L4" />
        </el-form-item>
        <el-form-item label="英文" prop="english">
          <el-input v-model="form.english" placeholder="请输入英文" />
        </el-form-item>
        <el-form-item label="物料组代码" prop="groupCode">
          <el-input v-model="form.groupCode" placeholder="请输入物料组代码" />
        </el-form-item>
        <el-form-item label="备注1" prop="remarkOne">
          <el-input v-model="form.remarkOne" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="大类(中文)" prop="bigClassChinese">
          <el-input v-model="form.bigClassChinese" placeholder="请输入大类(中文)" />
        </el-form-item>
        <el-form-item label="小类(中文)" prop="smallClassChinese">
          <el-input v-model="form.smallClassChinese" placeholder="请输入小类(中文)" />
        </el-form-item>
        <el-form-item label="大类" prop="bigClass">
          <el-input v-model="form.bigClass" placeholder="请输入大类" />
        </el-form-item>
        <el-form-item label="小类" prop="smallClass">
          <el-input v-model="form.smallClass" placeholder="请输入小类" />
        </el-form-item>
        <el-form-item label="次小类" prop="subCategory">
          <el-input v-model="form.subCategory" placeholder="请输入次小类" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterials, getMaterials, delMaterials, addMaterials, updateMaterials } from "@/api/gyl/materials";
import EasyTable from '@/components/EasyTable/index.vue'
import { getToken } from "../../../utils/auth";
export default {
  name: "Materials",
  components: {
    EasyTable
  },
  data() {
    return {
      opens:false,
      Shuju:{
        //表格字体大小
        fontSize:'12',
        Height:60,
      },
      //修改列表参数
      list:{
        ids:[],
        //点击某一列的字段名称
        itemName:'',
        afterName:'',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料分级分类表格数据
      materialsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: null,
        codeBefore4: null,
        materialClass1: null,
        itemSubclass2: null,
        itemSubclass3: null,
        materialSubclassOld4: null,
        materialSubclass4: null,
        english: null,
        groupCode: null,
        remarkOne: null,
        bigClassChinese: null,
        smallClassChinese: null,
        bigClass: null,
        smallClass: null,
        subCategory: null,
      },
      tableColumn: [
        //{ key: 0, type: 'seq', width: 60, fixed: null },
        { key: 1, type: 'checkbox', width: 50, fixed: null },
        { key: 2, field: 'sort', title: '序号', },
        { key: 3, field: 'codeBefore4', title: '编码前4位', },
        { key: 4, field: 'materialClass1', title: '物料L1', },
        { key: 5, field: 'itemSubclass2', title: '物料子类L2', },
        { key: 6, field: 'itemSubclass3', title: '物料子类L3', },
        { key: 7, field: 'remarkTwo', title: '备注2', },
        { key: 8, field: 'materialSubclass4', title: '物料小类L4', },
        { key: 9, field: 'english', title: '英文', },
        { key: 10, field: 'groupCode', title: '物料组代码', },
        { key: 11, field: 'remarkOne', title: '备注1', },
        { key: 12, field: 'bigClassChinese', title: '大类(中文)', },
        { key: 13, field: 'smallClassChinese', title: '小类(中文)', },
        { key: 14, field: 'updateBy', title: '大类', },
        { key: 15, field: 'updateBy', title: '小类', },
        { key: 16, field: 'subCategory', title: '次小类', },
      ],
            // 用户导入参数
            upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/gyl/materials/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料分级分类列表 */
    getList() {
      this.loading = true;
      listMaterials(this.queryParams).then(response => {
        this.materialsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sort: null,
        codeBefore4: null,
        materialClass1: null,
        itemSubclass2: null,
        itemSubclass3: null,
        materialSubclassOld4: null,
        materialSubclass4: null,
        english: null,
        groupCode: null,
        remarkOne: null,
        bigClassChinese: null,
        smallClassChinese: null,
        bigClass: null,
        smallClass: null,
        subCategory: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料分级分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaterials(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料分级分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaterials(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterials(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料分级分类编号为"' + ids + '"的数据项？').then(function () {
        return delMaterials(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gyl/materials/export', {
        ...this.queryParams
      }, `materials_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('gyl/materials/importTemplate', {
      }, `materials_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    getSelectData() {
      const childComputedValue = this.$refs.childRef.getSelectData();
      console.log(childComputedValue);
    },
    blurs(){
      if(this.Shuju.fontSize==''||this.Shuju.Height=='')
      {
        this.Shuju.Height=60
        this.Shuju.fontSize = 12
      }
      const Size =  this.$refs.childRef.changeSize();
      console.log(Size)
    },
    //获取表单数据
    rowsol(column){
      this.opens=true;
      this.list.itemName = this.getKebabCase(column.column)
      console.log(column.list)
      this.list.ids= column.List
    },
    //发送修改某一列请求
    submitList(){
      //此次更改请求的接口
      PutMaterialmaintenance(JSON.stringify(this.list)).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.opens = false;
        this.reset();
        this.getList();
      })
    },
    //将驼峰命名改成下划线
    getKebabCase(str) {
      let temp = str.replace(/[A-Z]/g, function(i) {
        return '_' + i.toLowerCase();
      })
      if (temp.slice(0,1) === '_') {
        temp = temp.slice(1);   //如果首字母是大写，执行replace时会多一个_，需要去掉
      }
      return temp;
    },
  }
};
</script>
<style lang="css" scoped>
@import "../CSS/style.css";
::v-deep .el-input__inner {
  height: 29px;
  line-height: 29px;
}
</style>
